<script>
	import store from '@/store'
	import SettingApi from '@/api/setting.js'
	import { userinfo } from '@/api/user.js'
	import storage from '@/utils/storage'
	import { ACCESS_TOKEN,APP_THEME } from '@/store/mutation-types'
	import platform from '@/core/platform'
	export default {
		onLaunch: function() {
			// #ifdef MP
			// 小程序主动更新
			this.updateManager()
			// #endif
			// 当前运行的终端
			store.commit('SET_PLATFORM', platform)
			// 用户认证token
			store.commit('SET_TOKEN',  uni.getStorageSync(ACCESS_TOKEN))
			// 设置全局自定义主题
			store.commit('SET_APP_THEME', storage.get(APP_THEME))
			SettingApi.data().then(data => {
				// 设置全局自定义主题
				store.dispatch('SetAppTheme', data.app_theme)
				// 设置参数
				delete data.app_theme
				store.dispatch('SetAppSetting', data)
			})
			userinfo(true).then(res => {
				store.dispatch('SetUserInfo', res)
				uni.setStorage({
					key: ACCESS_TOKEN,
					data: res.token
				});
			})
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
		  // 小程序主动更新
		  updateManager() {
		    const updateManager = uni.getUpdateManager()
		    updateManager.onCheckForUpdate(res => {
		      // 请求完新版本信息的回调
		      // console.log(res.hasUpdate)
		    })
		    updateManager.onUpdateReady(() => {
		      uni.showModal({
		        title: '更新提示',
		        content: '新版本已经准备好，即将重启应用',
		        showCancel: false,
		        success(res) {
		          if (res.confirm) {
		            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
		            updateManager.applyUpdate()
		          }
		        }
		      })
		    })
		    updateManager.onUpdateFailed(() => {
		      // 新的版本下载失败
		      uni.showModal({
		        title: '更新提示',
		        content: '新版本下载失败',
		        showCancel: false
		      })
		    })
		  }
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	.container {
		background-color: $page-bg;
		width: 100vw;
		min-height: 100vh;
	}
	::-webkit-scrollbar{
		  display: none;
	}
	.w-100 {
		width: 100%;
	}
	.h-100 {
		height: 100%;
	}
	.u-flex-column {
		flex-direction: column!important;
	}
	.flex-grow-1 {
	    flex-grow: 1!important;
	}
</style>
<style lang="scss">
	@import "./uni_modules/vk-uview-ui/index.scss";
</style>
